const enterpriseVue = new Vue({
  el: '#app',
  data() {
    return {
      pickerOptions: pickerOptions,
      rules: {
        name: [{
          required: true,
          message: '请输入栏目名称',
          trigger: 'blur',
        }],
        src: [{
          required: true,
          message: '请输入图片地址',
          trigger: 'blur',
        }],
        url: [{
          required: true,
          message: '请输入链接地址',
          trigger: 'blur',
        }],
        App_ID: [{
          required: true,
          message: '请输入小程序APP_ID',
          trigger: 'blur',
        }],
        AppID_Path_Type: [{
          required: true,
          message: '请选择所属区域',
          trigger: 'change'
        }]
      },
      fileList2: [{ name: 'food.jpeg', url: '../../../../admin/images/act.jpg' }],
      dialogImageUrl: '',
      dialogVisible: false,
      form: {
        ShopColumn_Id: '',
        name: '',
        src: '',
        url: '',
        App_ID: 'wx752cd7c5466abbaf',    //小程序appid
        Enterprise_Info_SerialNumber: $.cookie("enterprise"),
        AppID_Path_Type: "0"
      },
      src: '',
      loading: false,
      AddressList: [],
    }
  },
  mounted() {
    this.getAddress();
  },
  methods: {
    // 图片上传
    uploadFront(file) {
      let _this = this
      new Compressor(file, {
        quality: 1,
        // if(file.size) {
        // }
        success(file) {
          console.log(file.size)
          const formData = new FormData();
          formData.append('file', file, file.name);
          let oFileReader = new FileReader();
          oFileReader.readAsDataURL(file);
          oFileReader.onload = function (e) {
            _this.src = e.target.result
            $.ajax({
              type: 'POST',
              url: DomainName + '/FileListColumn',
              data: {
                Data: e.target.result,
                DataLength: file.size,
                FileName: file.name
              },
              complete: function (XMLHttpRequest) {
                _this.loading = false
                if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                  window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                }
              },
              success: function (Result) {
                if (Result.Status == "success") {
                  _this.form.src = Result.Picture_Url
                  _this.$message(Result.Picture_Url)
                } else {
                  _this.$message.error(Result.msg)
                }
              },
              error: function () {
                _this.$message.error("操作失败，请稍后再试")
              },
              dataType: "json"
            });
          }
        },
        error(err) {
          console.log(err.message);
        },
      });
    },
    //查看图片
    handlePictureCardPreview(file) {
      this.dialogVisible = true
      this.dialogImageUrl = file
    },
    //删除图片
    handleRemove(type, index) {
      if (type === 'front') {
        this.form.src = ''
      } else if (type === 'banner') {
        this.fileList = this.fileList.filter(function (item) {
          return item.Picture_Order != index
        })
      } else if (type === 'details') {
        this.fileList1 = this.fileList1.filter(function (item) {
          return item.Picture_Order != index
        })
      } else if (type === 'barcode') {
        this.fileList2 = this.fileList2.filter(function (item) {
          return item.Picture_Order != index
        })
      }
    },
    //查询路径
    getAddress() {
      const _this = this
      $.ajax({
        type: 'POST',
        url: DomainName + '/getInquiryAction?token=' + $.cookie("token"),//
        data: {
          Data: "enterprise_appid_path",
          isNullEnterprise:true
        },
        complete: function (XMLHttpRequest) {
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        success: function (Result) {
          _this.AddressList = Result;
          console.log(_this.AddressList);
          console.log(Result);
        },
        dataType: "json"
      });
    },
    submitForm(formName) {
      let _this = this
      console.log(this.form);
      this.$refs[formName].validate((valid) => {
        if (valid) {
          _this.loading = true
          $.ajax({
            url: DomainName + '/ShopColumnAdd',
            type: 'POST',
            data: {
              Data: JSON.stringify({ Shop_Column: [_this.form] }),
              token: $.cookie("token")
            },
            success: function (res) {
              console.log('提交结果', res)
              _this.$message.success("提交成功")
              window.location.href = './Classified.html'
            },
            error: function () {
              _this.$message.error("操作失败，请稍后再试")
            },
            complete: function (XMLHttpRequest) {
              _this.loading = false
              if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                window.parent.document.location.href = "../../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
              }
            }
          })
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
})